package com.topc.participant.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.topc.base.exception.Asserts;
import com.topc.base.model.PageParams;
import com.topc.base.model.PageResult;
import com.topc.base.utils.PageUtils;
import com.topc.participant.model.dto.QueryMenuInfoDto;
import com.topc.participant.model.po.MenuInfo;
import com.topc.participant.mapper.MenuInfoMapper;
import com.topc.participant.model.vo.MenuShowVo;
import com.topc.participant.model.vo.MenuTreeShowVo;
import com.topc.participant.service.IMenuInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 资源表 服务实现类
 * </p>
 *
 * @author Gogoing
 */
@Slf4j
@Service
public class MenuInfoServiceImpl extends ServiceImpl<MenuInfoMapper, MenuInfo> implements IMenuInfoService {

    @Override
    public List<MenuShowVo> getMenuShowList(int isParent) {
        LambdaQueryWrapper<MenuInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MenuInfo::getIsParent,isParent);
        List<MenuInfo> list = this.list(queryWrapper);
        List<MenuShowVo> collect = list.stream().map(menuInfo -> {
            MenuShowVo menuParentVo = new MenuShowVo();
            BeanUtil.copyProperties(menuInfo, menuParentVo);
            return menuParentVo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public PageResult<MenuInfo> searchMenuInfoByPage(PageParams pageParams, QueryMenuInfoDto queryMenuInfoDto) {
        LambdaQueryWrapper<MenuInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(ObjectUtil.isNotEmpty(queryMenuInfoDto.getMenuName()), MenuInfo::getMenuName,queryMenuInfoDto.getMenuName())
                .eq(queryMenuInfoDto.getParentId()!=-1,MenuInfo::getParentId,queryMenuInfoDto.getParentId());
        Page page = this.page(pageParams.page(), queryWrapper);
        return PageUtils.pageToResult(page);
    }

    @Override
    public void addMenuInfo(MenuInfo menuInfo) {
        boolean save = this.save(menuInfo);
        if (!save){
            Asserts.fail("保存失败");
        }
    }

    @Override
    public void delMenuInfo(Long menuId) {
        LambdaUpdateWrapper<MenuInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(MenuInfo::getParentId,menuId).or().eq(MenuInfo::getId,menuId);
        boolean remove = this.remove(updateWrapper);
        if (!remove){
            Asserts.fail("删除失败");
        }
    }

    @Override
    public List<MenuTreeShowVo> getMenuTree() {
        List<MenuInfo> list = this.list();
        Map<Long, MenuTreeShowVo> collect = list.stream().filter(menuInfo -> menuInfo.getIsParent() == 1)
                .map(MenuTreeShowVo::new).collect(Collectors.toMap(MenuTreeShowVo::getKey,
                        menuTreeShowVo -> menuTreeShowVo
                        , (value1, value2) -> value1));
        list.stream().filter(menuInfo -> menuInfo.getIsParent() == 0)
                .forEach(menuInfo -> {
                    MenuTreeShowVo menuTreeShowVo = collect.get(menuInfo.getParentId());
                    if (ObjectUtil.isEmpty(menuTreeShowVo)) return;
                    menuTreeShowVo.addChildren(new MenuTreeShowVo(menuInfo));
                });
        return collect.values().stream().collect(Collectors.toList());
    }
}
